﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using VCC.Model;
namespace VCC.Site
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            //routes.MapRoute(
            //    "AdSearchPaging",
            //    "{CateName}?page={id}", // URL with parameters
            //    new { controller = "Ad", action = "index" }
            //);
            routes.MapRoute(
                "LogInUser",
                "trang-dang-nhap", // URL with parameters
                new { controller = "User", action = "LogIn" }
            );
            routes.MapRoute(
                "RegisterUser",
                "dang-ky-nguoi-dung", // URL with parameters
                new { controller = "User", action = "Register" }
            );
            routes.MapRoute(
                "CreateAd",
                "dang-tin", // URL with parameters
                new { controller = "Ad", action = "Create" }
            );
            routes.MapRoute(
                "CreateAdsuccessful",
                "dang-tin-thanh-cong", // URL with parameters
                new { controller = "Ad", action = "CreateAdsuccessful" }
            );
            routes.MapRoute(
                "AdDetails",
                "{title}_{id}.html",
                new { controller = "Ad", action = "Details", id = 0 } // Parameter defaults
            );
            routes.MapRoute(
                "RewriteAdCategory",
                "{CateName}", // URL with parameters
                new { controller = "Ad", action = "index"}
            );
            routes.MapRoute(
                "RewriteAdSearch2",
                "Search/{shortCateName}/{shortFullSuburb}/", // URL with parameters
                new { controller = "Search", action = "index", shortCateName = "shortCateName", shortFullSuburb = "shortFullSuburb" }
            );
            routes.MapRoute(
                "RewriteAdSearch1",
                "Search/{shortCateName}/", // URL with parameters
                new { controller = "Search", action = "index" }
            );
            
            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );
            
        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            RegisterRoutes(RouteTable.Routes);

            //RouteTable.Routes.RouteExistingFiles = true;
            //RegisterRoutes(RouteTable.Routes);
            //RouteDebug.RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes);
            Application["OnlineUsers"] = 0;
        }
        protected void Application_End()
        {
            Application.Lock();
            if (!string.IsNullOrEmpty(Application["OnlineUsers"].ToString()))
            {
                Application["OnlineUsers"] = (int)Application["OnlineUsers"] - 1;
            }
            else
            {
                Application["OnlineUsers"] = 0;
            }
            Application.UnLock();
        }
        protected void Session_Start(object sender, EventArgs e)
        {
            Application.Lock();
            CounterManager.Counter();
            if (!string.IsNullOrEmpty(Application["OnlineUsers"].ToString()))
            {
                Application["OnlineUsers"] = (int)Application["OnlineUsers"] + 1;
            }
            else
            {
                Application["OnlineUsers"] = 1;
            }
            Application.UnLock();
            Session["LanguageID"] = "1";
        }

        protected void Session_End(object sender, EventArgs e)
        {
            Application.Lock();
            int c = int.Parse(Application["OnlineUsers"].ToString());
            if (c > 1)
            {
                Application["OnlineUsers"] = c-1;
            }
           Application.UnLock();
        }
    }
}